//
//  QueryRecordViewController.m
//  MyWallet
//
//  Created by 小肥羊 on 15/8/28.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "QueryRecordViewController.h"

static NSString *QueryRecordCellWithIdentifier = @"WalletRecordCell";

@interface QueryRecordViewController ()

@end

@implementation QueryRecordViewController
#pragma mark - 惰性初始化
- (NSMutableArray *)walletRecord
{
    if (!_walletRecord) {
        _walletRecord = [[NSMutableArray alloc] init];
    }
    return _walletRecord;
}

#pragma mark - Set 属性设置
- (void)setQueryObject:(NSString *)queryObject
{
    _queryObject = queryObject;
    [self setTitle:_queryObject];
}

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.walletRecord = [[WalletLocalData sharedManager] recordArrayWithCount:100 MoneyFrom:self.queryObject];
    [self.recordTableView reloadData];
}

- (void)createUI
{
    if (self.receiveDict[@"cardIndex"]) {
        NSString *cardIndex = [self.receiveDict objectForKey:@"cardIndex"];
        CardModel *card = [[TotalAssets sharedManager] cardWithIndexString:cardIndex];
        self.queryObject = card.cardName;
    } else {
        self.queryObject = Macro_MoneyFromWallet;
    }

}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletRecord.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0f;
}

/*! @brief Cell单元格 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WalletRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:QueryRecordCellWithIdentifier];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:QueryRecordCellWithIdentifier owner:nil options:nil];
        cell = [nibs lastObject];
    }
    NSDictionary *dict = self.walletRecord[indexPath.row];
    NSDate *recordDtate = dict[@"RecordDtate"];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    [cell.moneyFrom setText:dict[@"MoneyFor"]];
    [cell.remarks setText:dict[@"Remarks"]];
    [cell.recordDate setText:[dateformatter stringFromDate:recordDtate]];
    [cell moneyText:dict[@"Money"] Save:[dict[@"CheckoutType"] isEqualToString:@"1"]];
    return cell;
}

//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//进入编辑模式，按下出现的编辑按钮后
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setEditing:NO animated:YES];
    NSDictionary *dict = self.walletRecord[indexPath.row];
    if ([[WalletLocalData sharedManager] deleteRecord:dict[@"RecordDtate"]]) {
        [[TotalAssets sharedManager] reduction:self.queryObject Money:[dict[@"Money"] floatValue] IsSave:[dict[@"CheckoutType"] isEqualToString:@"0"]];
        [self.walletRecord removeObjectAtIndex:indexPath.row];
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView endUpdates];
    }
}

//修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"还原";
}

//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.walletRecord[indexPath.row];
    self.passDict = [[NSMutableDictionary alloc] initWithDictionary:dict];
    [self performSegueWithIdentifier:@"ChangeRecord" sender:self];
}

#pragma mark - 传参
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    WalletBaseViewController *send = segue.destinationViewController;
    send.receiveDict = self.passDict;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
